package com.liushao.controller.admin;
import com.liushao.entity.IntegralGrade;
import com.liushao.result.Result;
import com.liushao.service.IntegralGradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 积分等级表 前端控制器
 * </p>
 *
 * @author chen
 * @since 2025-01-03
 */
@RestController
@RequestMapping("/admin/core/integralGrade")
//@CrossOrigin//解决跨域的
@Api(value = "积分类",tags = "积分类")
public class IntegralGradeController {


    //http://localhost/admin/core/integralGrade/list

    @Autowired
    private IntegralGradeService integralGradeService;

    @ApiOperation("获取积分等级列表")
    @GetMapping("/list")
    public Result listAll(){
        List<IntegralGrade> integralGradeList = integralGradeService.list();
        return  Result.ok().data("list",integralGradeList);
    }


    @ApiOperation(value = "根据id删除积分等级数据",notes = "逻辑删除")
    @DeleteMapping("/remove/{id}")
    public  Result removeById(
            @ApiParam(value = "数据的idsss",required = true)
            @PathVariable("id") Long id
    ){

        boolean result = integralGradeService.removeById(id);
        if(result){
              return Result.ok().message("删除成功了");
        }else{
            return Result.error().message("删除失败了");
        }

    }


    @ApiOperation("新增积分等级")
    @PostMapping("/save")
    public Result save(
            @ApiParam(value = "积分等级对象",required = true)
            @RequestBody IntegralGrade integralGrade
    ){
        //给一个系统时间为更新时间!!!
        integralGrade.setUpdateTime(new Date());
        boolean result = integralGradeService.save(integralGrade);

        if(result){
            return Result.ok().message("保存成功了");
        }else{
            return Result.error().message("保存失败了");
        }
    }

    @ApiOperation("根据id获取积分等级")
    @GetMapping("/get/{id}")
    public Result getById(
            @ApiParam(value = "积分等级数据id",required = true)
            @PathVariable Long id
    ){

        IntegralGrade integralGrade = integralGradeService.getById(id);

        if(integralGrade!=null){
             return  Result.ok().data("record",integralGrade);
        }else{
            return  Result.error().message("数据不存在");
        }

    }

// http://localhost/admin/core/integralGrade/update
    @ApiOperation("更新积分等级信息")
    @PutMapping("/update")
    public Result updateById(
            @ApiParam(value = "积分等级对象",required = true)
            @RequestBody IntegralGrade integralGrade
    ){

        boolean result = integralGradeService.updateById(integralGrade);

        if(result){
            return Result.ok().message("修改成功了");
        }else{
            return Result.error().message("修改失败了");
        }

    }







}

